//
//  BaseNaviController.m
//  deerkids
//
//  Created by 王昭的Mac Air on 2017/8/28.
//  Copyright © 2017年 wangzhao. All rights reserved.
//

#import "BaseNaviController.h"
#import "MyButton.h"
#import "UIImage+ImageWithColor.h"

@interface BaseNaviController ()

@end

@implementation BaseNaviController

//在该类调用时只执行一次，且最先执行
+(void)initialize
{
    //设置导航栏控制器文字和图片的填充色
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    
    //只能设置titleView文字颜色
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
    //设置导航控制器背景颜色
    //导航栏存在色差的原因是因为透明度的关系
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation"] forBarMetrics:UIBarMetricsDefault];
    
    [[UINavigationBar appearance] setTranslucent:NO];
    
    [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(BgTitleColor)];
    
    
    /*
     总结：
     设置导航栏的背景颜色的时候，使用  [[UINavigationBar appearance] setBarTintColor:NavigationColor]; 会使颜色变浅，与设计图有误差
     解决办法是在设计图上截取部分图片，然后利用setBackgroundImage方法
     */
}

//重写导航栏控制器的push方法
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    //当导航栏栈内的元素>0时，创建返回按钮
    if (self.viewControllers.count > 0) {
        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back_title"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
        
        //push的时候隐藏标签控制器
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:YES];
}

-(void)back
{
    [self.view endEditing:YES];  //结束编辑
    [self popViewControllerAnimated:YES];
}



// 支持设备自动旋转
- (BOOL)shouldAutorotate
{
    return YES;
}
// 支持横竖屏显示
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

- (void)createPlaceholderView:(NSString *)title message:(NSString *)message image:(UIImage *)image withView:(UIView *)superView action:(void (^)(UIButton *button)) clickblock{
    if (_placeholderView) {
        [_placeholderView removeFromSuperview];
        _placeholderView = nil;
    }
    if(superView==nil){
        superView=self.view;
    }
    
    _placeholderView = [[UIView alloc]initWithFrame:superView.bounds];
    
    
    [_placeholderView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin];
    [_placeholderView setAutoresizesSubviews:YES];
    [_placeholderView setBackgroundColor:[UIColor clearColor]];
    //    [_placeholderView setBackgroundColor:UIColorFromRGB(BgSystemColor)];
    [superView addSubview:_placeholderView];
    CGFloat y = 0;
    CGRect pf = CGRectMake(0, 0, superView.bounds.size.width, 0);
    if(image){
        UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"network_fail"]];
        
        [icon setImage:image];
        
        [icon setContentMode:UIViewContentModeCenter];
        [icon setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        icon.frame = CGRectMake(0,0, pf.size.width, image.size.height);
        [_placeholderView addSubview:icon];
        y= icon.frame.size.height+20;
    }
    if(title){
        UILabel *lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(0, y, pf.size.width, 30)];
        [lblTitle setText:title];
        [lblTitle setFont:[UIFont systemFontOfSize:k12FontSize]];
        [lblTitle setTextColor:DEEP_GRAY_COLOR];
        [lblTitle setTextAlignment:NSTextAlignmentCenter];
        [lblTitle setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        [lblTitle setNumberOfLines:0];
        [lblTitle setBackgroundColor:[UIColor clearColor]];
        [_placeholderView addSubview:lblTitle];
        y=y+30+5;
    }
    
    if(message){
        UILabel *lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(0, y, pf.size.width, 30)];
        [lblTitle setText:message];
        [lblTitle setFont:[UIFont systemFontOfSize:k12FontSize]];
        [lblTitle setTextColor:DEEP_GRAY_COLOR];
        [lblTitle setTextAlignment:NSTextAlignmentCenter];
        [lblTitle setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        [lblTitle setAutoresizesSubviews:YES];
        [lblTitle setBackgroundColor:[UIColor clearColor]];
        [_placeholderView addSubview:lblTitle];
        y=y+25;
    }
    
    // 如果有重新加载方法，就显示重新加载按钮
    if(clickblock){
        MyButton *reButton=[MyButton buttonWithType:UIButtonTypeCustom];
        [reButton setFrame:CGRectMake(pf.size.width/2-192/2, y+5, 192, 50)];
        [reButton setBackgroundImage:[UIImage imageWithColor:BASE_COLOR] forState:UIControlStateNormal];
        [reButton setBackgroundImage:[UIImage imageWithColor:BASE_COLOR] forState:UIControlStateDisabled];
        [reButton setTitle:@"点击重试" forState:UIControlStateNormal];
        [reButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [reButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        [reButton.titleLabel setFont:[UIFont systemFontOfSize:k12FontSize]];
        reButton.objTag = clickblock;
        [reButton addTarget:self action:@selector(reloadButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [reButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin];
        reButton.layer.cornerRadius  = 5;
        reButton.layer.masksToBounds = YES;
        [_placeholderView addSubview:reButton];
        
        
        UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        activityIndicator.tag=10;
        [activityIndicator setFrame:CGRectMake(192/2-45, 15, 20, 20)];
        [reButton addSubview:activityIndicator];
        
        y=y+60;
    }
    pf.size.height= y;
    
    [_placeholderView setFrame:pf];
    [_placeholderView setCenter:CGPointMake(superView.center.x, superView.bounds.size.height/2-80)];
}

-(void)reloadButtonClick:(MyButton *)btn{
    [btn setTitle:@"      加载中..." forState:UIControlStateNormal];
    // 变灰
    //    [btn setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(BgVCodeColor)] forState:UIControlStateDisabled];
    
    UIActivityIndicatorView *activityIndicator = [btn viewWithTag:10];
    [activityIndicator startAnimating];
    ((void (^)(UIButton *button))btn.objTag)(btn);
}


- (void)removePlaceholderView{
    if (_placeholderView && _placeholderView!=nil) {
        [_placeholderView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [_placeholderView removeFromSuperview];
        
        _placeholderView = nil;
    }
}


@end
